package cn.it.shop.dao.impl;

import java.util.List;

import org.hibernate.classic.Session;
import org.springframework.stereotype.Repository;

import cn.it.shop.dao.GoodsDao;
import cn.it.shop.pojo.Goods;

@Repository(value = "goodsDao")
@SuppressWarnings("unchecked")
public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements
		GoodsDao {

	@Override
	public List<Goods> queryByCommendAndOpen(int cid) {
		String hql = "FROM Goods g JOIN FETCH g.category WHERE g.commend=true AND g.open=true AND "
				+ "g.category.id=:cid ORDER BY g.date DESC";
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery(hql) //
				.setInteger("cid", cid)//
				.setFirstResult(0) //
				.setMaxResults(4) //
				.list();
	}

	@Override  //AOP 再切目标对象的连接点时，只能切入 pubilc的方法,不能切入继承方法
	public void save(Goods t) {
		super.save(t);

	}

	@Override
	public List<Goods> queryByName(String name) {
		if(name.length()==0){
			throw new NullPointerException("空异常");
		}else{
			throw new NumberFormatException("格式不正确!");
		}
	}
}
